<template>
  <div class="api-manage-container">
    <div class="toolbar">
      <el-button type="primary" @click="handleCreateApi">
        <el-icon><Plus /></el-icon>新建接口
      </el-button>
      <el-input
        v-model="searchQuery"
        placeholder="搜索接口名称"
        class="search-input"
      >
        <template #prefix>
          <el-icon><Search /></el-icon>
        </template>
      </el-input>
    </div>

    <el-table :data="apiList" style="width: 100%" v-loading="loading">
      <el-table-column prop="name" label="接口名称" />
      <el-table-column prop="path" label="接口路径" />
      <el-table-column prop="method" label="请求方法">
        <template #default="{ row }">
          <el-tag :type="getMethodTagType(row.method)">{{ row.method }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template #default="{ row }">
          <el-switch
            v-model="row.status"
            :active-value="1"
            :inactive-value="0"
            @change="handleStatusChange(row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="{ row }">
          <el-button-group>
            <el-button type="primary" link @click="handleEdit(row)">
              编辑
            </el-button>
            <el-button type="primary" link @click="handleTest(row)">
              测试
            </el-button>
            <el-button type="danger" link @click="handleDelete(row)">
              删除
            </el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :total="total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { Plus, Search } from "@element-plus/icons-vue";
import { ElMessage, ElMessageBox } from "element-plus";

const loading = ref(false);
const searchQuery = ref("");
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(100);

const apiList = ref([
  {
    id: 1,
    name: "获取用户信息",
    path: "/api/user/info",
    method: "GET",
    status: 1,
  },
  {
    id: 2,
    name: "更新用户信息",
    path: "/api/user/update",
    method: "POST",
    status: 1,
  },
]);

const getMethodTagType = (method: string) => {
  const types: Record<string, string> = {
    GET: "success",
    POST: "primary",
    PUT: "warning",
    DELETE: "danger",
  };
  return types[method] || "info";
};

const handleCreateApi = () => {
  // TODO: 实现创建接口逻辑
};

const handleEdit = (row: any) => {
  // TODO: 实现编辑接口逻辑
};

const handleTest = (row: any) => {
  // TODO: 实现测试接口逻辑
};

const handleDelete = (row: any) => {
  ElMessageBox.confirm("确定要删除该接口吗？", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    // TODO: 实现删除接口逻辑
    ElMessage.success("删除成功");
  });
};

const handleStatusChange = (row: any) => {
  // TODO: 实现状态更改逻辑
};

const handleSizeChange = (val: number) => {
  pageSize.value = val;
  // TODO: 重新加载数据
};

const handleCurrentChange = (val: number) => {
  currentPage.value = val;
  // TODO: 重新加载数据
};
</script>

<style scoped>
.api-manage-container {
  padding: 20px;
}

.toolbar {
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
}

.search-input {
  width: 300px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style> 